//
//  WNCompassView.m
//  bjbus
//
//  Created by 王建华 on 2024/6/3.
//

#import "WNCompassView.h"
#import "WNCompassViewCollectionViewCell.h"

@interface WNCompassView () <UICollectionViewDelegate, UICollectionViewDataSource>

@property (weak, nonatomic) IBOutlet UICollectionView *compassCollectionView;
@property (weak, nonatomic) IBOutlet UILabel *headLabel;
@property (weak, nonatomic) IBOutlet UILabel *head2Label;

@property (nonatomic, assign) NSInteger heading;

@end

@implementation WNCompassView

+ (instancetype)compassView {
    
    return [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil].firstObject;
}

- (void)awakeFromNib {
    [super awakeFromNib];

    self.compassCollectionView.delegate = self;
    self.compassCollectionView.dataSource = self;
    [self.compassCollectionView registerNib:[UINib nibWithNibName:NSStringFromClass([WNCompassViewCollectionViewCell class]) bundle:nil] forCellWithReuseIdentifier:NSStringFromClass([WNCompassViewCollectionViewCell class])];

}

- (void)compassViewDidUpdateHeading:(CLLocationDirection)newHeading {
    
    CGFloat headingScreen = (self.frame.size.width * 0.5) / 90 * 30;//屏幕一半可以展示的度数
    CGFloat adjustAngle = headingScreen - 15; // 修正角度 collection 偏移到0度需要的度数

    CGFloat offsetBase = self.compassCollectionView.contentSize.width * 0.5;
    CGFloat offsetX = offsetBase / 360.f * (newHeading-adjustAngle);
    self.headLabel.text = [NSString stringWithFormat:@"%.0f˚", newHeading];
    self.head2Label.text = [WNCompassView getDirectionsFormHeading:newHeading];
    
    if (offsetX <= 15) {
        offsetX = offsetX + offsetBase;
    }
    self.compassCollectionView.contentOffset = CGPointMake(offsetX, 0);
    
    //NSLog(@"trueHeading:%f\nmagneticHeading%f\n屏幕一半可以展示的度数：%f", newHeading.trueHeading, newHeading.magneticHeading, headingScreen);
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 24;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    WNCompassViewCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([WNCompassViewCollectionViewCell class]) forIndexPath:indexPath];
    cell.index = indexPath;
    return cell;
}

+ (NSString *)getDirectionsFormHeading:(double)heading {
    
    NSString *direction = @"北";
    if (heading <= 22 && heading > 337 ) {
        return @"北";
    }else if (heading <= 66 && heading > 22) {
        return @"东北";
    }else if (heading > 67 && heading <= 112) {
        return @"东";
    }else if (heading > 112 && heading <= 156) {
        return @"东南";
    }else if (heading > 156 && heading <= 202) {
        return @"南";
    }else if (heading > 202 && heading <= 246) {
        return @"西南";
    }else if (heading > 246 && heading <= 292) {
        return @"西";
    }else if (heading > 292 && heading <= 337) {
        return @"西北";
    }
    
    return direction;
}
@end
